﻿using Decoration.IService;
using Decoration.Models;
using Decoration.Models.Dto;
using Decoration.Models.DTO;
using Decoration.Models.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Decoration.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DictinfoController : ControllerBase
    {
        IDictInfoService svc;
        public DictinfoController(IDictInfoService _svc)
        {
            this.svc = _svc;
        }

        // GET: api/<DictinfoController>
        [HttpGet]
        public PagedResult<DictInfoDto> Query(string? DictName, int DictTypeId = 0, int PageSize = 10, int PageIndex = 1)
        {
            var SQL = @"select  b.*,a.DictTypeName
		                            from Base_DictTypes a inner join Base_DictInfos b
		                            on a.Id=b.DictTypeId
                                    where 1=1 ";
            if (!string.IsNullOrEmpty(DictName))
            {
                SQL += @" and b.DictName like @DictName ";
            }
            if (DictTypeId > 0)
            {
                SQL += " and DictTypeId = @DictTypeId ";
            }
            //计算总条数
            var SQLCount = @"select Count(*) num from (" + SQL + ")a ";
            var total = svc.ExecuteScalar<int>(SQLCount, new { DictName = "%" + DictName + "%", DictTypeId = DictTypeId });
            SQL += @"  order by b.Id desc
            offset  @PageSize*(@PageIndex-1)rows fetch next  @PageSize  rows only;";
            var list = svc.Query<DictInfoDto>(SQL, new { DictName = "%" + DictName + "%", DictTypeId = DictTypeId, PageSize, PageIndex }).ToList();

            return new PagedResult<DictInfoDto>() { code = 1, msg = "成功", total = total, data = list };
        }

        // GET api/<DictinfoController>/5
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var info = svc.Get(o => o.Id == id);
            if (info == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            return new Result() { code = 1, msg = "成功", data = info };

        }

        // POST api/<DictinfoController>
        [HttpPost]
        public Result Post(Base_DictInfo dict)
        {
            var info = new Base_DictInfo()
            {
                CreateTime = DateTime.Now,
                DictName = dict.DictName,
                DictDesc = dict.DictDesc,
                DictOrder = dict.DictOrder,
                DictTypeId = dict.DictTypeId,
                DictValue = dict.DictValue,
                Status = 1
            };
            if (info == null)
            {
                return new Result() { code = -1, msg = "数据不能为空" };
            }
            var res=svc.Add(dict);
            return new Result() { code = 1,msg="成功",data=res };
        }

        // PUT api/<DictinfoController>/5
        [HttpPut]
        public Result Put(Base_DictInfo dict)
        {
            var getId = svc.Get(o=>o.Id==dict.Id);
            if (getId == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            getId.DictValue = dict.DictValue;
            getId.DictDesc = dict.DictDesc;
            getId.DictOrder = dict.DictOrder;
            getId.DictTypeId = dict.DictTypeId;
            getId.DictName = dict.DictName;
            getId.Status = dict.Status;
            var res=svc.Update(getId);
            return new Result() { code = 1, msg = "成功", data = res };

        }

        // DELETE api/<DictinfoController>/5
        [HttpDelete("{id}")]
        public Result Delete(int id)
        {
            var getId = svc.Get(o=>o.Id==id);
            if (getId == null)
            {
                return new Result() { code = -1, msg = "为查询到数据" };
            }
            getId.Status = 0;
            var res = svc.Update(getId);
            return new Result() { code = 1, msg = "成功", data = getId };

        }
        /// <summary>
        /// 根据字典类型名查询字典
        /// </summary>
        /// <param name="DictTypeName"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("QueryTypeName")]
        public Result<List<DictDto>> DictTypeStatus(string DictTypeName)
        {
            var sql = @"select di.*,dt.DictTypeName from Base_DictInfos di 
                        inner join Base_DictTypes dt on di.DictTypeId = dt.Id
                        where di.Status =1 and DictTypeName = @DictTypeName";
            var list = svc.Query<DictDto>(sql, new { DictTypeName = DictTypeName }).ToList();
            return new Result<List<DictDto>>() { code = 1, msg = "成功", data = list };
        }
        [HttpGet]
        [Route ("TypeName")]
        public Result TypeName()
        {
            var data = svc.Query(o => o.Status == 1);
            return new Result() { code = 1, data = data };
        }
    }
}
